{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys; sys.path.append(\"C:/GitWorks/pdb-profiling\")\n",
    "from pdb_profiling import default_config\n",
    "from pdb_profiling.processors import PDB\n",
    "from pdb_profiling.utils import DisplayPDB\n",
    "from pdb_profiling.viewer import NGL\n",
    "\n",
    "default_config(\"C:/GitWorks/pdb-profiling/test/demo\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "6934c1148b784674897bb4cee7fafba3",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": []
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import nglview\n",
    "import ipywidgets\n",
    "from pandas import DataFrame\n",
    "from unsync import unsync"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "        <style>\n",
       "            img.display {\n",
       "                -webkit-filter: invert(1);\n",
       "                filter: invert(1);\n",
       "                }\n",
       "        </style>\n",
       "    \n",
       "        <table align=\"center\">\n",
       "            <tr>\n",
       "            \n",
       "                <td>\n",
       "                    <b>Asymmetric unit</b> of 10gs\n",
       "                </td>\n",
       "    \n",
       "                <td>\n",
       "                    <b>Biological assembly 1</b> of 10gs\n",
       "                </td>\n",
       "    \n",
       "            </tr>\n",
       "            <tr>\n",
       "            \n",
       "                <td>\n",
       "                    <img class=\"display\" width=\"300em\" src=\"https://cdn.rcsb.org/images/structures/0g/10gs/10gs_model-1.jpeg\"/>\n",
       "                </td>\n",
       "    \n",
       "                <td>\n",
       "                    <img class=\"display\" width=\"300em\" src=\"https://cdn.rcsb.org/images/structures/0g/10gs/10gs_assembly-1.jpeg\"/>\n",
       "                </td>\n",
       "    \n",
       "            </tr>\n",
       "        </table>\n",
       "        "
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "pdb_id = '10gs'\n",
    "pdb_ob = PDB(pdb_id)\n",
    "pdb_ob.set_assembly().result()\n",
    "DisplayPDB(dark=True).show(pdb_ob.pdb_id, pdb_ob.assembly)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>entity_id_1</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>chain_id_1</th>\n",
       "      <td>A</td>\n",
       "      <td>A</td>\n",
       "      <td>B</td>\n",
       "      <td>A</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>struct_asym_id_1</th>\n",
       "      <td>A</td>\n",
       "      <td>A</td>\n",
       "      <td>B</td>\n",
       "      <td>A</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>struct_asym_id_in_assembly_1</th>\n",
       "      <td>A</td>\n",
       "      <td>A</td>\n",
       "      <td>B</td>\n",
       "      <td>A</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>asym_id_rank_1</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>model_id_1</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>molecule_type_1</th>\n",
       "      <td>polypeptide(L)</td>\n",
       "      <td>polypeptide(L)</td>\n",
       "      <td>polypeptide(L)</td>\n",
       "      <td>polypeptide(L)</td>\n",
       "      <td>polypeptide(L)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>surface_range_1</th>\n",
       "      <td>[[2,13],[15,19],[22,71],[74,95],[97,132],[134,...</td>\n",
       "      <td>[[2,13],[15,19],[22,71],[74,95],[97,132],[134,...</td>\n",
       "      <td>[[2,13],[15,19],[22,71],[74,95],[97,132],[134,...</td>\n",
       "      <td>[[2,13],[15,19],[22,71],[74,95],[97,132],[134,...</td>\n",
       "      <td>[[2,13],[15,19],[22,71],[74,95],[97,132],[134,...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>interface_range_1</th>\n",
       "      <td>[[48,49],[51,51],[59,64],[66,67],[70,71],[74,7...</td>\n",
       "      <td>[[7,8],[10,10],[12,13],[35,35],[38,39],[44,44]...</td>\n",
       "      <td>[[7,8],[10,10],[12,13],[35,35],[38,39],[44,44]...</td>\n",
       "      <td>[[22,22],[26,30],[192,192],[195,195],[197,197]]</td>\n",
       "      <td>[[22,22],[26,30],[192,192],[195,195],[197,197]]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>entity_id_2</th>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>chain_id_2</th>\n",
       "      <td>B</td>\n",
       "      <td>A</td>\n",
       "      <td>B</td>\n",
       "      <td>A</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>struct_asym_id_2</th>\n",
       "      <td>B</td>\n",
       "      <td>C</td>\n",
       "      <td>E</td>\n",
       "      <td>D</td>\n",
       "      <td>F</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>struct_asym_id_in_assembly_2</th>\n",
       "      <td>B</td>\n",
       "      <td>C</td>\n",
       "      <td>E</td>\n",
       "      <td>D</td>\n",
       "      <td>F</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>asym_id_rank_2</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>model_id_2</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>molecule_type_2</th>\n",
       "      <td>polypeptide(L)</td>\n",
       "      <td>bound</td>\n",
       "      <td>bound</td>\n",
       "      <td>bound</td>\n",
       "      <td>bound</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>surface_range_2</th>\n",
       "      <td>[[2,13],[15,19],[22,71],[74,95],[97,132],[134,...</td>\n",
       "      <td>[[1,1]]</td>\n",
       "      <td>[[1,1]]</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>interface_range_2</th>\n",
       "      <td>[[48,49],[51,51],[59,64],[66,67],[70,71],[74,7...</td>\n",
       "      <td>[[1,1]]</td>\n",
       "      <td>[[1,1]]</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>pdb_id</th>\n",
       "      <td>10gs</td>\n",
       "      <td>10gs</td>\n",
       "      <td>10gs</td>\n",
       "      <td>10gs</td>\n",
       "      <td>10gs</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>assembly_id</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>interface_id</th>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>7</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>use_au</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                                              0  \\\n",
       "entity_id_1                                                                   1   \n",
       "chain_id_1                                                                    A   \n",
       "struct_asym_id_1                                                              A   \n",
       "struct_asym_id_in_assembly_1                                                  A   \n",
       "asym_id_rank_1                                                                1   \n",
       "model_id_1                                                                    1   \n",
       "molecule_type_1                                                  polypeptide(L)   \n",
       "surface_range_1               [[2,13],[15,19],[22,71],[74,95],[97,132],[134,...   \n",
       "interface_range_1             [[48,49],[51,51],[59,64],[66,67],[70,71],[74,7...   \n",
       "entity_id_2                                                                   1   \n",
       "chain_id_2                                                                    B   \n",
       "struct_asym_id_2                                                              B   \n",
       "struct_asym_id_in_assembly_2                                                  B   \n",
       "asym_id_rank_2                                                                1   \n",
       "model_id_2                                                                    1   \n",
       "molecule_type_2                                                  polypeptide(L)   \n",
       "surface_range_2               [[2,13],[15,19],[22,71],[74,95],[97,132],[134,...   \n",
       "interface_range_2             [[48,49],[51,51],[59,64],[66,67],[70,71],[74,7...   \n",
       "pdb_id                                                                     10gs   \n",
       "assembly_id                                                                   0   \n",
       "interface_id                                                                  1   \n",
       "use_au                                                                    False   \n",
       "\n",
       "                                                                              1  \\\n",
       "entity_id_1                                                                   1   \n",
       "chain_id_1                                                                    A   \n",
       "struct_asym_id_1                                                              A   \n",
       "struct_asym_id_in_assembly_1                                                  A   \n",
       "asym_id_rank_1                                                                1   \n",
       "model_id_1                                                                    1   \n",
       "molecule_type_1                                                  polypeptide(L)   \n",
       "surface_range_1               [[2,13],[15,19],[22,71],[74,95],[97,132],[134,...   \n",
       "interface_range_1             [[7,8],[10,10],[12,13],[35,35],[38,39],[44,44]...   \n",
       "entity_id_2                                                                   2   \n",
       "chain_id_2                                                                    A   \n",
       "struct_asym_id_2                                                              C   \n",
       "struct_asym_id_in_assembly_2                                                  C   \n",
       "asym_id_rank_2                                                                1   \n",
       "model_id_2                                                                    1   \n",
       "molecule_type_2                                                           bound   \n",
       "surface_range_2                                                         [[1,1]]   \n",
       "interface_range_2                                                       [[1,1]]   \n",
       "pdb_id                                                                     10gs   \n",
       "assembly_id                                                                   0   \n",
       "interface_id                                                                  2   \n",
       "use_au                                                                    False   \n",
       "\n",
       "                                                                              2  \\\n",
       "entity_id_1                                                                   1   \n",
       "chain_id_1                                                                    B   \n",
       "struct_asym_id_1                                                              B   \n",
       "struct_asym_id_in_assembly_1                                                  B   \n",
       "asym_id_rank_1                                                                1   \n",
       "model_id_1                                                                    1   \n",
       "molecule_type_1                                                  polypeptide(L)   \n",
       "surface_range_1               [[2,13],[15,19],[22,71],[74,95],[97,132],[134,...   \n",
       "interface_range_1             [[7,8],[10,10],[12,13],[35,35],[38,39],[44,44]...   \n",
       "entity_id_2                                                                   2   \n",
       "chain_id_2                                                                    B   \n",
       "struct_asym_id_2                                                              E   \n",
       "struct_asym_id_in_assembly_2                                                  E   \n",
       "asym_id_rank_2                                                                1   \n",
       "model_id_2                                                                    1   \n",
       "molecule_type_2                                                           bound   \n",
       "surface_range_2                                                         [[1,1]]   \n",
       "interface_range_2                                                       [[1,1]]   \n",
       "pdb_id                                                                     10gs   \n",
       "assembly_id                                                                   0   \n",
       "interface_id                                                                  3   \n",
       "use_au                                                                    False   \n",
       "\n",
       "                                                                              3  \\\n",
       "entity_id_1                                                                   1   \n",
       "chain_id_1                                                                    A   \n",
       "struct_asym_id_1                                                              A   \n",
       "struct_asym_id_in_assembly_1                                                  A   \n",
       "asym_id_rank_1                                                                1   \n",
       "model_id_1                                                                    1   \n",
       "molecule_type_1                                                  polypeptide(L)   \n",
       "surface_range_1               [[2,13],[15,19],[22,71],[74,95],[97,132],[134,...   \n",
       "interface_range_1               [[22,22],[26,30],[192,192],[195,195],[197,197]]   \n",
       "entity_id_2                                                                   3   \n",
       "chain_id_2                                                                    A   \n",
       "struct_asym_id_2                                                              D   \n",
       "struct_asym_id_in_assembly_2                                                  D   \n",
       "asym_id_rank_2                                                                1   \n",
       "model_id_2                                                                    1   \n",
       "molecule_type_2                                                           bound   \n",
       "surface_range_2                                                             NaN   \n",
       "interface_range_2                                                           NaN   \n",
       "pdb_id                                                                     10gs   \n",
       "assembly_id                                                                   0   \n",
       "interface_id                                                                  7   \n",
       "use_au                                                                    False   \n",
       "\n",
       "                                                                              4  \n",
       "entity_id_1                                                                   1  \n",
       "chain_id_1                                                                    B  \n",
       "struct_asym_id_1                                                              B  \n",
       "struct_asym_id_in_assembly_1                                                  B  \n",
       "asym_id_rank_1                                                                1  \n",
       "model_id_1                                                                    1  \n",
       "molecule_type_1                                                  polypeptide(L)  \n",
       "surface_range_1               [[2,13],[15,19],[22,71],[74,95],[97,132],[134,...  \n",
       "interface_range_1               [[22,22],[26,30],[192,192],[195,195],[197,197]]  \n",
       "entity_id_2                                                                   3  \n",
       "chain_id_2                                                                    B  \n",
       "struct_asym_id_2                                                              F  \n",
       "struct_asym_id_in_assembly_2                                                  F  \n",
       "asym_id_rank_2                                                                1  \n",
       "model_id_2                                                                    1  \n",
       "molecule_type_2                                                           bound  \n",
       "surface_range_2                                                             NaN  \n",
       "interface_range_2                                                           NaN  \n",
       "pdb_id                                                                     10gs  \n",
       "assembly_id                                                                   0  \n",
       "interface_id                                                                  8  \n",
       "use_au                                                                    False  "
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "@unsync\n",
    "async def get_interface_df(pdb_ob):\n",
    "    molecule_types = (\n",
    "        'polypeptide(L)',\n",
    "        'polypeptide(D)',\n",
    "        'polydeoxyribonucleotide',\n",
    "        'polyribonucleotide',\n",
    "        'polydeoxyribonucleotide/polyribonucleotide hybrid',\n",
    "        'bound',\n",
    "        'carbohydrate_polymer')\n",
    "    return DataFrame([i async for i in pdb_ob.pipe_interface_res_dict(\n",
    "        func='pipe_protein_else_interface', \n",
    "        molecule_types=molecule_types, \n",
    "        allow_same_class_interaction=True)])\n",
    "    \n",
    "interface_df = get_interface_df(pdb_ob).result()\n",
    "interface_df.head().T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "rep = [{\"type\": \"all\", \"params\":{\"assembly\": \"AU\"}}]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "views = [NGL.get_interface_view(\n",
    "    nglview.NGLWidget(nglview.FileStructure(f'C:/Users/Nature/Downloads/{pdb_id}.cif') , representation=rep), \n",
    "    interface_df.loc[i]) for i in range(4)]\n",
    "# background='#272822'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "5e9f7ad1cb76445599bca50512f2fc17",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "GridBox(children=(NGLWidget(background='#F3F3F3', n_components=1), NGLWidget(background='#F3F3F3', n_component…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "ipywidgets.GridBox(views, layout=ipywidgets.Layout(grid_template_columns='1fr 1fr'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "view = nglview.show_file(f'C:/Users/Nature/Downloads/{pdb_id}.cif')\n",
    "view.representation = rep\n",
    "_ = [NGL.get_interface_view(view, interface_df.loc[i], background='#272822', surface_opacity_1=0.05, surface_opacity_2=0.05, surface_color_1='#F0F8FF', surface_color_2='#F0F8FF') for i in range(4)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "355070c78bf0429da7dcbcc485db06b2",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "NGLWidget(background='#272822', n_components=1)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "view"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
